#include "EditorVariableAbilityView.h"

#include "Widgets/Positioners/VerticalPositionerWidget.h"
#include "Widgets/ImageWidget.h"
#include "Widgets/TextWidget.h"
#include "Widgets/RectangleWidget.h"
#include "Widgets/Mouse/MouseWidget.h"
#include "Widgets/CanvasWidget.h"

void REditorVariableAbilityView::BindViewModel(TSharedObjectPtr<REditorAbilityViewModel> InViewModel)
{
    Super::BindViewModel(InViewModel);
    VariableViewModel = InViewModel;
}

void REditorVariableAbilityView::FillOperations(TSharedObjectPtr<RWidget> InTitleBar)
{
}


void REditorVariableAbilityView::CreateWidget(TSharedObjectPtr<RWidget> InParent)
{
    auto Root=NewWidget<RWidget>(InParent);
    Root->Anchor.Fill(InParent);

    List=NewWidget<RVerticalPositionerWidget>(Root);
    List->Anchor.Fill(Root);

    VariableViewModel->OnListChanged.Bind([this]()
    {
        UpdateList();
    });    

    UpdateList();
}


void REditorVariableAbilityView::UpdateList()
{
    List->ClearSubWidgets();


    for(int64_t i=0;i<VariableViewModel->GetNodeNum();i++)
    {
        auto Line=NewWidget<RWidget>(List);
        Line->Height= VariableViewModel->LineHeight;
        Line->Anchor.LeftReference=List;
        Line->Anchor.RightReference=List;

        //with indent
        auto LineContent=NewWidget<RWidget>(Line);
        LineContent->Anchor.VerticalFill(Line);
        LineContent->Anchor.LeftReference=Line;
        LineContent->Anchor.LeftOffset=VariableViewModel->GetNodeIndent(i);
        LineContent->Anchor.RightReference=Line;

        const float ExpendIconSize=VariableViewModel->LineHeight;
        if(VariableViewModel->HasNodeChildren(i))
        {
            auto ExpendIcon=NewWidget<RImageWidget>(LineContent);
            ExpendIcon->Width=ExpendIconSize;
            ExpendIcon->Height=ExpendIconSize;
            ExpendIcon->Image=VariableViewModel->GetNodeIcon(i);

            auto Mouse = NewWidget<RMouseWidget>(Line);
            Mouse->Anchor.Fill(Line);
            Mouse->OnClicked.BindLambda([this,i](const SMouseButtonEvent&)
            {
                VariableViewModel->ToggleNodeExpanded(i);
                return true;
            });
            
        }

        if(VariableViewModel->IsNodeScope(i))
        {
            auto Name=NewWidget<RTextWidget>(LineContent);
            Name->Text=VariableViewModel->GetNodeName(i);
            Name->Anchor.LeftReference=LineContent;
            Name->Anchor.LeftOffset=ExpendIconSize;
            Name->Anchor.RightReference=LineContent;
            Name->Anchor.VerticalFill(LineContent);

            continue;
        }

        //variable node
        auto NameAndValue=NewWidget<RCanvasWidget>(LineContent);
        NameAndValue->Anchor.LeftReference=LineContent;
        NameAndValue->Anchor.LeftOffset=ExpendIconSize;
        NameAndValue->Anchor.RightReference=LineContent;
        NameAndValue->Anchor.VerticalFill(LineContent);
        
        CString Name=VariableViewModel->GetNodeName(i);
        CString Value=VariableViewModel->GetNodeValue(i);
        
        NameAndValue->AddColorfulText(
            SVec2(0,0),
            Name+U":"+Value,
            {
                SColorfulTextColor(0,SColor::FromHexRGB(0x905F80)),
                SColorfulTextColor(Name.GetLength()+1,SColor(0.8,0.8,0.8,1)),
            }
            ,16
            ,nullptr
        );

    }

}
